package handlers

import (
	"bloggo/bloggo"
	"bloggo/dto"
	"bloggo/service"
	"bloggo/structs"
	"gitee.com/chunanyong/zorm"
	"github.com/gin-gonic/gin"
	"strconv"
)

// Albums 相册
func Albums(c *gin.Context) *bloggo.R {
	page := zorm.NewPage()
	page.PageNo, _ = strconv.Atoi(c.DefaultQuery("current", "1"))
	page.PageSize, _ = strconv.Atoi(c.DefaultQuery("size", "10"))
	finder := zorm.NewFinder()
	finder.Append("SELECT * FROM").Append(structs.TbPhotoAlbumStructTableName).Append("ORDER BY create_time DESC")
	var data []structs.TbPhotoAlbumStruct
	err := zorm.Query(c, finder, &data, page)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.OkData(data)
}

// PhotosByAlbumId 根据相册id查看照片列表
func PhotosByAlbumId(c *gin.Context) *bloggo.R {
	albumId := c.Param("albumId")
	if albumId == "" {
		return bloggo.FailMessage("相册不存在")
	}
	photoDTO := dto.PhotoDTO{}
	// 查询相册信息
	photoAlbum, err := service.FindTbPhotoAlbumStructById(c, albumId)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	if photoAlbum.IsDelete == 1 || photoAlbum.Status == 2 {
		return bloggo.FailMessage("相册不存在")
	}
	photoDTO.PhotoAlbumCover = photoAlbum.AlbumCover
	photoDTO.PhotoAlbumName = photoAlbum.AlbumName
	// 查询照片列表
	page := zorm.NewPage()
	page.PageNo, _ = strconv.Atoi(c.DefaultQuery("current", "1"))
	page.PageSize, _ = strconv.Atoi(c.DefaultQuery("size", "10"))
	finder := zorm.NewFinder().Append("SELECT id,photo_compress,photo_src FROM").Append(structs.TbPhotoStructTableName)
	finder.Append("WHERE album_id=? AND is_delete=?", albumId, 0).Append("ORDER BY id DESC")
	err = zorm.Query(c, finder, &photoDTO.PhotoList, page)
	if err != nil {
		return bloggo.FailMessage(err.Error())
	}
	return bloggo.OkData(photoDTO)
}
